package com.wandian.util;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author long
 * @Create 2012-10-5 下午4:27:55
 * @QQ 108232706
 * @version 1.0
 * @description BaseTimerTask
 */
public class BaseTimerTask extends TimerTask {

	private boolean boolStart;
	private final Timer timer;
	private int count;

	public BaseTimerTask(String name, boolean isDaemon) {
		timer = new Timer(name, isDaemon);
	}

	public BaseTimerTask() {
		this("", false);
	}

	/**
	 * 指定一个重复间隔的任务
	 * 
	 * @param delay
	 * @param period
	 */
	public void startTask(long delay, long period) {
		timer.schedule(this, delay, period);
		boolStart = true;
	}

	public void startTask(long delay) {
		timer.schedule(this, delay);
		boolStart = true;
	}

	public void startTask(Date firstTime, long period) {
		timer.schedule(this, firstTime, period);
		boolStart = true;
	}

	public void startTask(Date time) {
		timer.schedule(this, time);
		boolStart = true;
	}

	public void stopTask() {
		boolStart = false;
		cannel();
	}

	private void cannel() {
		timer.cancel();
		this.cancel();
		System.out.println(timer.toString() + " ThreadName:" + Thread.currentThread().getName() + " stop");
	}

	@Override
	public void run() {
		// runTask();
		// if (!boolStart) {
		// cannel();
		// }
		// System.gc();
	}

	public void runTask() {
		System.out.println(count++);
	}

	public boolean isBoolStart() {
		return boolStart;
	}

	public void setBoolStart(boolean boolStart) {
		this.boolStart = boolStart;
	}

}
